home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / csystray.000 / csystray / csystray.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  2.7 KB  |  127 lines

  1. // csystray.cpp : Implementation file for the CWin95SystemTray class.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "systray.h"
  6. #include "csystray.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. #define STF_AUTOUNREGISTER      0x00000001
  14.  
  15.  
  16. CWin95SystemTray::CWin95SystemTray(HWND hWnd,
  17.     UINT uCallbackMsgID)
  18. {
  19.     Init();
  20.     Attach(hWnd);
  21.     EnableAutoUnregister();
  22. }
  23.  
  24. CWin95SystemTray::~CWin95SystemTray()
  25. {
  26.     if ((m_dwFlags & STF_AUTOUNREGISTER) && (m_NotifyIconData.hWnd))
  27.     {
  28.         ItemDelete(m_NotifyIconData.uID);
  29.     }
  30.  
  31.     Init();
  32. }
  33.  
  34. BOOL CWin95SystemTray::Attach(HWND hWnd)
  35. {
  36.     m_NotifyIconData.hWnd = hWnd;
  37.     return(hWnd ? TRUE : FALSE);
  38. }
  39.  
  40. void CWin95SystemTray::EnableAutoUnregister(BOOL bEnable)
  41. {
  42.     if (bEnable)
  43.     {
  44.         m_dwFlags |= STF_AUTOUNREGISTER;
  45.     }
  46.     else
  47.     {
  48.         m_dwFlags &= ~(STF_AUTOUNREGISTER);
  49.     }
  50. }
  51.  
  52. void CWin95SystemTray::RegisterCallbackMessage(UINT uMsgID)
  53. {
  54.     m_NotifyIconData.uCallbackMessage = uMsgID;
  55. }
  56.  
  57. HWND CWin95SystemTray::Detach(HWND hWnd)
  58. {
  59.     HWND hOldWnd = m_NotifyIconData.hWnd;
  60.     m_NotifyIconData.hWnd = hWnd;
  61.     return(hOldWnd);
  62. }
  63.  
  64. BOOL CWin95SystemTray::ItemAdd(UINT uID)
  65. {
  66.     return(SystemTrayMessage(m_NotifyIconData.hWnd, NIM_ADD,
  67.         uID, NULL, NULL));
  68. }
  69.  
  70. BOOL CWin95SystemTray::ItemDelete(UINT uID)
  71. {
  72.     return(SystemTrayMessage(m_NotifyIconData.hWnd, NIM_DELETE,
  73.         uID, NULL, NULL));
  74. }
  75.  
  76. BOOL CWin95SystemTray::ItemModify(UINT uID,
  77.     HICON hIcon,
  78.     LPCSTR pszBubbleText)
  79. {
  80.     return(SystemTrayMessage(m_NotifyIconData.hWnd, NIM_MODIFY,
  81.         uID, hIcon, pszBubbleText));
  82. }
  83.  
  84. BOOL CWin95SystemTray::ItemSetBubbleText(UINT uID,
  85.     LPCSTR pszBubbleText)
  86. {
  87.     return(ItemModify(uID, NULL, pszBubbleText));
  88. }
  89.  
  90. void CWin95SystemTray::Init(void)
  91. {
  92.     m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
  93.     m_NotifyIconData.hWnd = NULL;
  94.     m_NotifyIconData.uID = 0;
  95.     m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  96.     m_NotifyIconData.uCallbackMessage = 0;
  97.     m_NotifyIconData.hIcon = NULL;
  98.     m_NotifyIconData.szTip[0] = '\0';
  99.     m_dwFlags = 0;
  100. }
  101.  
  102. BOOL CWin95SystemTray::SystemTrayMessage(HWND hWnd,
  103.     DWORD dwMessage,
  104.     UINT uID,
  105.     HICON hIcon,
  106.     LPCSTR pszBubbleText)
  107. {
  108.     BOOL bRet;
  109.     
  110.     m_NotifyIconData.hWnd = hWnd;
  111.     m_NotifyIconData.uID = uID;
  112.     m_NotifyIconData.hIcon = hIcon;
  113.  
  114.     if (pszBubbleText != NULL)
  115.     {
  116.         lstrcpyn(m_NotifyIconData.szTip, pszBubbleText,
  117.             sizeof(m_NotifyIconData.szTip));
  118.     }
  119.     else
  120.     {
  121.         m_NotifyIconData.szTip[0] = '\0';
  122.     }
  123.  
  124.     bRet = Shell_NotifyIcon(dwMessage, &m_NotifyIconData);
  125.     return(bRet);
  126. }
  127.